﻿using System.Linq;
using System.Xml.Linq;
using NUnit.Framework;
using WB.Mvc.Forms;

namespace WB.Forms.Tests
{
	[TestFixture]
	public class FormBaseTestFixture
	{
		[Test]
		public void Has_Default_ButtonText()
		{
			var stub = new FormStub("name");

			Assert.IsNotNull(stub.ButtonText, "ButtonText should not be null.");
			Assert.IsNotEmpty(stub.ButtonText, "ButtonText should not be empty.");
		}

		[Test]
		public void Has_Submit_Button()
		{
			var stub = new FormStub("name");

			XElement ele = XElement.Parse(stub.AsDiv());

			XElement button =
				ele.Descendants("input").SingleOrDefault(
					x => x.Attribute("type") != null && x.Attribute("type").Value.Equals("submit"));

			Assert.IsNotNull(button, "Expected to find a submit button.");
			Assert.AreEqual("submit", button.Attribute("type").Value);
		}

		[Test]
		public void Submit_Has_ButtonText()
		{
			var stub = new FormStub("name");

			XElement ele = XElement.Parse(stub.AsDiv());

			XElement button = ele.Descendants("input")
				.SingleOrDefault(x => x.Attribute("type") !=
				                      null && x.Attribute("type").Value.Equals("submit"));

			Assert.IsNotNull(button, "Prerequisite of this test is that a submit exists, and one was not found.");
			Assert.IsNotNull(button.Attribute("value"), "Button did not contain a default value.");
			Assert.AreEqual(stub.ButtonText, button.Attribute("value").Value);
		}

		public class FormStub : FormBase
		{
			public FormStub(string name) : base(name)
			{
				base.FieldGroups[0].Fields.Add(new CharField("name"));
			}
		}
	}
}